﻿using MT.CustomUserControl.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MT.CustomUserControl.Views.UserControls
{
    /// <summary>
    /// LoginUserControl2.xaml 的交互逻辑
    /// </summary>
    public partial class LoginUserControl2 : UserControl
    {
        private LoginUcViewModel loginUcViewModel;

        public LoginUserControl2()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Btn_Password.Content = this.FindResource("PasswordIcon");
            textBox_password.Visibility = Visibility.Collapsed;
            password.Visibility = Visibility.Visible;

            if (DataContext != null)
            {
                loginUcViewModel = (LoginUcViewModel)DataContext;

                //清空用户名和密码
                loginUcViewModel.UserName = string.Empty;
                loginUcViewModel.UserPassword = string.Empty;
            }
        }

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex reges = new Regex(@"^[A-Za-z0-9]+$");
            bool re = reges.IsMatch(e.Text);
            if (!re)
                e.Handled = true;
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                loginUcViewModel.LoginAction?.Invoke(loginUcViewModel.UserName, loginUcViewModel.UserPassword);
            }
        }

        private void TextBox_PasswordBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex reges = new Regex(@"^[A-Za-z0-9]+$");
            bool re = reges.IsMatch(e.Text);
            if (!re)
                e.Handled = true;
        }

        private void ComboBox_Users_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var uie = e.OriginalSource as UIElement;
                uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                e.Handled = true;
            }
        }

        private void Btn_Password_Click(object sender, RoutedEventArgs e)
        {
            if (textBox_password.Visibility == Visibility.Collapsed)
            {
                Btn_Password.Content = this.FindResource("PasswordIconOpen");
                textBox_password.Visibility = Visibility.Visible;
                password.Visibility = Visibility.Collapsed;
            }
            else
            {
                Btn_Password.Content = this.FindResource("PasswordIcon");
                textBox_password.Visibility = Visibility.Collapsed;
                password.Visibility = Visibility.Visible;
            }
        }
    }
}
